package com.example.xinwenkehuduan;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.smtt.sdk.WebView;

import java.util.Date;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobDate;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.gdcp.Demo.Comment;
import cn.gdcp.Demo.UserLike;

public class NewDetail extends AppCompatActivity {
    private WebView wvNewDetail;
    private EditText edSay;
    private Button btnSend;
    private ImageView ivLike;
    private String newsid;
    private String newsjson;
    private boolean islike = false;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_detail);


        final String url =getIntent().getStringExtra("URL");
        final String title = getIntent().getStringExtra("TITLE");
        newsid = getIntent().getStringExtra("NEWSID");
        newsjson = getIntent().getStringExtra("NEWSJSON");
        wvNewDetail = findViewById(R.id.wb_newdetail);

        setTitle(title);
        wvNewDetail.loadUrl(url);



        edSay = findViewById(R.id.et_say);
        btnSend = findViewById(R.id.btn_send);
        ivLike = findViewById(R.id.iv_like);




        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String say = edSay.getText().toString().trim();
                if (say.length()==0){
                    Toast.makeText(NewDetail.this,"请你输入评论内容",Toast.LENGTH_SHORT).show();
                    return;
                }
                isLogin();

                final String userId = BmobUser.getCurrentUser(BmobUser.class).getObjectId();
                Comment comment = new Comment();
                comment.setUserID(userId);
                comment.setNewsid(newsid);
                comment.setNewsjson(newsjson);
                comment.setComment(say);
                comment.setTime(new BmobDate(new Date()));
                comment.save(new SaveListener<String>() {
                    @Override
                    public void done(String s, BmobException e) {
                        if (e==null){
                            Toast.makeText(NewDetail.this,"评论发表成功",Toast.LENGTH_SHORT).show();

                        }
                    }
                });
            }
        });

        ivLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断用户是否登陆了  没登陆 就先去登陆

               isLogin();


                final String userId = BmobUser.getCurrentUser(BmobUser.class).getObjectId();

                //查询是否有该新闻的收藏数据，有的话就更新，没有就添加
                BmobQuery<UserLike> userLikeBmobQuery = new BmobQuery<>();
                userLikeBmobQuery.addWhereEqualTo("userID",userId);
                userLikeBmobQuery.addWhereEqualTo("newsID",newsid);

                userLikeBmobQuery.findObjects(new FindListener<UserLike>() {
                    @Override
                    public void done(List<UserLike> list, BmobException e) {
                        if (e==null){
                            // 说明已经有了针对该新闻的收藏数据
                            if (list != null &&list.size()>0){
                                UserLike userLike = list.get(0);
                                userLike.setLike(!islike);
                                sendBroadcast(new Intent().setAction("LIKE_CHANGED"));
                                userLike.update(new UpdateListener() {
                                    @Override
                                    public void done(BmobException e) {
                                        if (e==null){
                                            islike = !islike;
                                            sendBroadcast(new Intent().setAction("LIKE_CHANGED"));
                                            if (islike){
                                                ivLike.setImageResource(R.drawable.shoucang);
                                            }else {
                                                ivLike.setImageResource(R.drawable.weishoucang);
                                            }
                                        }
                                    }
                                });
                            }else {
                                // 如果没有  新建数据  保存
                                UserLike userLike = new UserLike();
                                userLike.setNewsID(newsid);
                                userLike.setNewsJSON(newsjson);
                                userLike.setUserID(userId);
                                userLike.save(new SaveListener<String>() {
                                    @Override
                                    public void done(String s, BmobException e) {
                                        if (e==null){
                                            Toast.makeText(NewDetail.this,"收藏成功",Toast.LENGTH_SHORT).show();
                                            ivLike.setImageResource(R.drawable.shoucang);
                                        }
                                    }
                                });
                            }
                        }
                    }
                });





            }

        });

        //查询当前新闻的收藏状态
        queryNewLike();






    }

    private void isLogin() {
        //判断用户是否登陆了  没登陆 就先去登陆

        if (BmobUser.getCurrentUser(BmobUser.class) ==null){
            Intent intent = new Intent(NewDetail.this,LoginActivity.class);

            startActivity(intent);
            return;
        }

    }

    private void queryNewLike() {
        // 如果用户未登录 ，则不用查询
        if (BmobUser.getCurrentUser(BmobUser.class)==null){
            return;
        }

        String userid = BmobUser.getCurrentUser(BmobUser.class).getObjectId();


        BmobQuery<UserLike> userLikeBmobQuery = new BmobQuery<>();
        userLikeBmobQuery.addWhereEqualTo("userID",userid);
        userLikeBmobQuery.addWhereEqualTo("newsID",newsid);
        userLikeBmobQuery.addWhereEqualTo("isLike",true);

        userLikeBmobQuery.findObjects(new FindListener<UserLike>() {
            @Override
            public void done(List<UserLike> list, BmobException e) {
                if (e == null){
                    if (list != null && list.size()>0){
                            islike = true;
                            ivLike.setImageResource(R.drawable.shoucang);
                    }
                }
            }
        });


    }



}
